# SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
# SPDX-FileCopyrightText: 2010-2024 EfficiOS Inc.

TOP_LTTNG_MODULES_DIR := $(shell dirname $(lastword $(MAKEFILE_LIST)))/../..

include $(TOP_LTTNG_MODULES_DIR)/src/Kbuild.common

ccflags-y += -I$(TOP_LTTNG_MODULES_DIR)/include

obj-$(CONFIG_LTTNG) += lttng-probe-sched.o
obj-$(CONFIG_LTTNG) += lttng-probe-irq.o
obj-$(CONFIG_LTTNG) += lttng-probe-timer.o

# A dependency on internal header 'mm/slab.h' was introduced in v6.0
kmem_dep = $(srctree)/mm/slab.h
kmem_dep_wildcard = $(wildcard $(kmem_dep))
kmem_dep_check = $(shell \
if [ \( $(VERSION) -ge 7 \
   -o \( $(VERSION) -eq 6 -a $(PATCHLEVEL) -ge 0 \) \
   -o \( $(VERSION) -eq 5 -a $(PATCHLEVEL) -ge 14 -a $(RHEL_API_VERSION) -gt 1622301 \) \) -a \
   -z "$(kmem_dep_wildcard)" ] ; then \
  echo "warn" ; \
else \
  echo "ok" ; \
fi ;)
ifeq ($(kmem_dep_check),ok)
  obj-$(CONFIG_LTTNG) += lttng-probe-kmem.o
else
  $(warning File $(kmem_dep) not found. Probe "kmem" is disabled. Use full kernel source tree to enable it.)
endif

obj-$(CONFIG_LTTNG) += lttng-probe-module.o
obj-$(CONFIG_LTTNG) += lttng-probe-power.o
obj-$(CONFIG_LTTNG) += lttng-probe-statedump.o

ifneq ($(CONFIG_NET_9P),)
  obj-$(CONFIG_LTTNG) += lttng-probe-9p.o
endif # CONFIG_NET_9P

i2c_dep = $(srctree)/include/trace/events/i2c.h
ifneq ($(wildcard $(i2c_dep)),)
  obj-$(CONFIG_LTTNG) += lttng-probe-i2c.o
endif

ifneq ($(CONFIG_KVM),)
  obj-$(CONFIG_LTTNG) += lttng-probe-kvm.o
  ifneq ($(CONFIG_X86),)
    ifneq ($(CONFIG_KALLSYMS_ALL),)
      kvm_dep_lapic = $(srctree)/arch/x86/kvm/lapic.h
      kvm_dep_lapic_check = $(wildcard $(kvm_dep_lapic))
      ifneq ($(kvm_dep_lapic_check),)
        kvm_dep_iodev = $(srctree)/include/kvm/iodev.h
        kvm_dep_iodev_check = $(wildcard $(kvm_dep_iodev))
        ifneq ($(kvm_dep_iodev_check),)
          kvm_dep_emulate = $(srctree)/arch/x86/kvm/kvm_emulate.h
          kvm_dep_emulate_wildcard = $(wildcard $(kvm_dep_emulate))
          kvm_dep_emulate_check = $(shell \
          if [ \( $(VERSION) -ge 6 \
             -o \( $(VERSION) -eq 5 -a $(PATCHLEVEL) -eq 7 \) \
             -o \( $(VERSION) -eq 4 -a $(PATCHLEVEL) -eq 18 -a $(RHEL_API_VERSION) -ge 2400000 \) \) -a \
             -z "$(kvm_dep_emulate_wildcard)" ] ; then \
            echo "warn" ; \
          else \
            echo "ok" ; \
          fi ;)
          ifeq ($(kvm_dep_emulate_check),ok)
            CFLAGS_lttng-probe-kvm-x86.o += -I$(srctree)/virt/kvm \
                       -I$(srctree)/arch/x86/kvm
            CFLAGS_lttng-probe-kvm-x86-mmu.o += -I$(srctree)/virt/kvm \
                       -I$(srctree)/arch/x86/kvm
            obj-$(CONFIG_LTTNG) += lttng-probe-kvm-x86.o
            obj-$(CONFIG_LTTNG) += lttng-probe-kvm-x86-mmu.o
          else # ($(kvm_dep_emulate_check),ok)
            $(warning File $(kvm_dep_emulate) not found. Probe "kvm" x86-specific is disabled. Use full kernel source tree to enable it.)
          endif # ($(kvm_dep_emulate_check),ok)
        else # $(kvm_dep_iodev_check)
          $(warning File $(kvm_dep_iodev) not found. Probe "kvm" x86-specific is disabled. Use full kernel source tree to enable it.)
        endif # $(kvm_dep_iodev_check)
      else # $(kvm_dep_lapic_check)
        $(warning File $(kvm_dep_lapic) not found. Probe "kvm" x86-specific is disabled. Use full kernel source tree to enable it.)
      endif # $(kvm_dep_lapic_check)
    else
      $(warning CONFIG_KALLSYMS_ALL is disabled. Probe "kvm" x86-specific is disabled. Rebuild your kernel with this configuration option enabled in order to trace this subsystem.)
    endif # CONFIG_KALLSYMS_ALL
  endif # CONFIG_X86
endif # CONFIG_KVM

ifneq ($(CONFIG_X86),)
  x86_irq_vectors_dep = $(srctree)/arch/x86/include/asm/trace/irq_vectors.h

  ifneq ($(wildcard $(x86_irq_vectors_dep)),)
    obj-$(CONFIG_LTTNG) += lttng-probe-x86-irq-vectors.o
  endif # $(wildcard $(x86_irq_vectors_dep))

  x86_exceptions_dep = $(srctree)/arch/x86/include/asm/trace/exceptions.h

  ifneq ($(wildcard $(x86_exceptions_dep)),)
    obj-$(CONFIG_LTTNG) += lttng-probe-x86-exceptions.o
  endif # $(wildcard $(x86_exceptions_dep))
endif # CONFIG_X86

obj-$(CONFIG_LTTNG) += lttng-probe-signal.o

ifneq ($(CONFIG_DMA_SHARED_BUFFER),)
  obj-$(CONFIG_LTTNG) += $(shell \
    if [ $(VERSION) -ge 5 \
       -o \( $(VERSION) -eq 4 -a $(PATCHLEVEL) -ge 10 \) ] ; then \
      echo "lttng-probe-dma-fence.o" ; fi;)
endif # CONFIG_DMA_SHARED_BUFFER

ifneq ($(CONFIG_BLOCK),)
  # need blk_cmd_buf_len
  ifneq ($(CONFIG_EVENT_TRACING),)
    obj-$(CONFIG_LTTNG) += lttng-probe-block.o
  endif # CONFIG_EVENT_TRACING
endif # CONFIG_BLOCK

ifneq ($(CONFIG_NET),)
  obj-$(CONFIG_LTTNG) += lttng-probe-napi.o
  obj-$(CONFIG_LTTNG) += lttng-probe-skb.o
  obj-$(CONFIG_LTTNG) += lttng-probe-net.o
  obj-$(CONFIG_LTTNG) += lttng-probe-sock.o
  obj-$(CONFIG_LTTNG) += lttng-probe-udp.o
endif # CONFIG_NET

ifneq ($(CONFIG_SND_SOC),)
  obj-$(CONFIG_LTTNG) += lttng-probe-asoc.o
endif # CONFIG_SND_SOC

ifneq ($(CONFIG_GPIOLIB),)
  obj-$(CONFIG_LTTNG) += lttng-probe-gpio.o
endif # CONFIG_GPIOLIB

ifneq ($(CONFIG_JBD2),)
  obj-$(CONFIG_LTTNG) += lttng-probe-jbd2.o
endif # CONFIG_JBD2

ifneq ($(CONFIG_JBD),)
  obj-$(CONFIG_LTTNG) += lttng-probe-jbd.o
endif # CONFIG_JBD

ifneq ($(CONFIG_REGULATOR),)
  obj-$(CONFIG_LTTNG) += lttng-probe-regulator.o
endif # CONFIG_REGULATOR

ifneq ($(CONFIG_SCSI),)
  obj-$(CONFIG_LTTNG) += lttng-probe-scsi.o
endif # CONFIG_SCSI

obj-$(CONFIG_LTTNG) += lttng-probe-vmscan.o

# lock probe does not work, so disabling it for now
#ifneq ($(CONFIG_LOCKDEP),)
#  obj-$(CONFIG_LTTNG) += lttng-probe-lock.o
#endif # CONFIG_LOCKDEP

ifneq ($(CONFIG_BTRFS_FS),)
  btrfs_dep = $(srctree)/fs/btrfs/*.h
  ifneq ($(wildcard $(btrfs_dep)),)
    obj-$(CONFIG_LTTNG) += lttng-probe-btrfs.o
  else
    $(warning Files $(btrfs_dep) not found. Probe "btrfs" is disabled. Use full kernel source tree to enable it.)
  endif # $(wildcard $(btrfs_dep))
endif # CONFIG_BTRFS_FS

ifneq ($(CONFIG_COMPACTION),)
  # A dependency on internal header 'mm/internal.h' was introduced in v5.18
  compaction_dep = $(srctree)/mm/internal.h
  compaction_dep_wildcard = $(wildcard $(compaction_dep))
  compaction_dep_check = $(shell \
  if [ \( $(VERSION) -ge 6 \
     -o \( $(VERSION) -eq 5 -a $(PATCHLEVEL) -ge 18 \) \
     -o \( $(VERSION) -eq 5 -a $(PATCHLEVEL) -ge 14 -a $(RHEL_API_VERSION) -gt 1622301 \) \) -a \
     -z "$(compaction_dep_wildcard)" ] ; then \
    echo "warn" ; \
  else \
    echo "ok" ; \
  fi ;)
  ifeq ($(compaction_dep_check),ok)
    obj-$(CONFIG_LTTNG) += lttng-probe-compaction.o
  else
    $(warning Files $(compaction_dep) not found. Probe "compaction" is disabled. Use full kernel source tree to enable it.)
  endif # $(wildcard $(compaction_dep))
endif # CONFIG_COMPACTION

ifneq ($(CONFIG_EXT4_FS),)
  ext4_dep = $(srctree)/fs/ext4/*.h
  ifneq ($(wildcard $(ext4_dep)),)
    obj-$(CONFIG_LTTNG) += lttng-probe-ext4.o
  else
    $(warning Files $(ext4_dep) not found. Probe "ext4" is disabled. Use full kernel source tree to enable it.)
  endif # $(wildcard $(ext4_dep))
endif # CONFIG_EXT4_FS

obj-$(CONFIG_LTTNG) += lttng-probe-printk.o

ifneq ($(CONFIG_FRAME_WARN),0)
  CFLAGS_lttng-probe-printk.o += -Wframe-larger-than=2200
endif

# Introduced in v3.6, remove in v5.18
random_dep = $(srctree)/include/trace/events/random.h
ifneq ($(wildcard $(random_dep)),)
  obj-$(CONFIG_LTTNG) += lttng-probe-random.o
endif

obj-$(CONFIG_LTTNG) += lttng-probe-rcu.o

ifneq ($(CONFIG_REGMAP),)
  regmap_dep = $(srctree)/drivers/base/regmap/trace.h
  ifneq ($(wildcard $(regmap_dep)),)
    obj-$(CONFIG_LTTNG) += lttng-probe-regmap.o
  else
    $(warning File $(regmap_dep) not found. Probe "regmap" is disabled. Use full kernel source tree to enable it.)
  endif # $(wildcard $(regmap_dep)),
endif # CONFIG_REGMAP

ifneq ($(CONFIG_PM_RUNTIME),)
  obj-$(CONFIG_LTTNG) += lttng-probe-rpm.o
endif # CONFIG_PM_RUNTIME

ifneq ($(CONFIG_SUNRPC),)
  obj-$(CONFIG_LTTNG) += lttng-probe-sunrpc.o
endif # CONFIG_SUNRPC

ifneq ($(CONFIG_VIDEO_V4L2),)
  obj-$(CONFIG_LTTNG) += lttng-probe-v4l2.o
endif # CONFIG_VIDEO_V4L2

obj-$(CONFIG_LTTNG) += lttng-probe-workqueue.o

ifneq ($(CONFIG_KALLSYMS_ALL),)
  obj-$(CONFIG_LTTNG) += lttng-probe-writeback.o
else
  ifdef CONFIG_LOCALVERSION # Check if dot-config is included.
    $(warning CONFIG_KALLSYMS_ALL is disabled, therefore probe "writeback" is disabled. Rebuild your kernel with this configuration option enabled in order to trace this subsystem.)
  endif
endif # CONFIG_KALLSYMS_ALL

ifneq ($(CONFIG_PREEMPTIRQ_EVENTS),)
  obj-$(CONFIG_LTTNG) += lttng-probe-preemptirq.o
endif # CONFIG_PREEMPTIRQ_EVENTS

# vim:syntax=make
